package com.dykj.fireFighting.web.controller;

import com.dykj.fireFighting.api.base.devHisResult.entity.DevHisResult;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.api.monitoringCenter.dto.DevHisFireDto;
import com.dykj.fireFighting.api.monitoringCenter.service.IDevHisFireService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 火灾自动报警系统设备运行记录
 *
 * @Author fgc
 * @Date 2021-06-15 16:52:12
 */
@Slf4j
@Api(value = "火灾自动报警系统设备运行记录接口", tags = {"火灾自动报警系统设备运行记录接口"})
@RestController
@RequestMapping("/devHisFire")
public class DevHisFireController {
    @DubboReference
    private IDevHisFireService devHisFireService;
    @DubboReference
    private EsDevHisService esDevHisService;
    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private ISysDirectoryService directoryService;
    @DubboReference
    private IDevHisResultService devHisResultService;

    /**
     * 火灾自动报警系统设备运行记录列表
     *
     * @return
     */
    @AutoLog(value = "火灾自动报警系统设备运行记录-分页列表查询")
    @ApiOperation(value = "火灾自动报警系统设备运行记录分页列表", notes = "火灾自动报警系统设备运行记录分页列表")
    @GetMapping("/list")
    @RequiresPermissions("devHisFire/list")
    public CommonResult<PageVo<EsDevHis>> treeGrid(DevHisFireDto devHisFireDto) {
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        PageVo<EsDevHis> pageVo = esDevHisService.centerSearch(enterpriseId.toString(), devHisFireDto.getSocialUnitId(), devHisFireDto.getName(),
                (int) devHisFireDto.getPage(), (int) devHisFireDto.getLimit(),devHisFireDto.getBeginDate(),devHisFireDto.getEndDate(),devHisFireDto.getLx());
        return CommonResult.success(pageVo);
    }

    /**
     * 获取火灾自动报警系统设备运行记录信息
     */
    /*@AutoLog(value = "火灾自动报警系统设备运行记录-获取VIEW信息")
    @ApiOperation(value = "获取火灾自动报警系统设备运行记录信息", notes = "获取火灾自动报警系统设备运行记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "Long", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("devHis/devHisFire/info")
    public CommonResult<DevHisFire> info(@PathVariable("id") Long id) {
        DevHisFire devHisFire = devHisFireService.getById(id);
        return CommonResult.success(devHisFire);
    }*/

    @AutoLog(value = "区域的统计模块")
    @GetMapping("/qytj")
    //@RequiresPermissions("fireInformation/list")
    public CommonResult<Map<String,Object>> qytj(String benginTime, String endTime) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        List<Enterprise> list=enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        List<Map<String,String>> strList=new ArrayList<>();
        List<String> nameList=new ArrayList<>();
        List<String> codeList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SysDirectory sysDirectory=directoryService.queryByCode(list.get(i).getAreaCode().toString());
            String name=sysDirectory.getValue();
            String code=sysDirectory.getCode();
            if(!nameList.contains(name)){
                Map<String,String> map=new HashMap<>();
                map.put("code",code);
                map.put("name",name);
                strList.add(map);
                nameList.add(name);
                codeList.add(code);
            }
        }
        Map<String,Object> resuleMap=new HashMap<>();
        resuleMap.put("qy",strList);
        List<Integer> hj=new ArrayList<>();
        List<Integer> gz=new ArrayList<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String,String> map=strList.get(i);
            List<Enterprise> listEnterprise=enterpriseService.queryListByAreaCodeOne(map.get("code").toString(),0);
            List<String> eIdList=new ArrayList<>();
            for (int j = 0; j < listEnterprise.size(); j++) {
                if(codeList.contains(listEnterprise.get(j).getAreaCode())){
                    eIdList.add(listEnterprise.get(j).getId().toString());
                }
            }
            DevHisResult devHisResult=devHisResultService.getCenterNum(eIdList,benginTime,endTime);
            if(null==devHisResult){
                hj.add(0);
                gz.add(0);
            }else{
                hj.add(devHisResult.getFireAlarm());
                gz.add(devHisResult.getFault());
            }

        }
        resuleMap.put("hj",hj);
        resuleMap.put("gz",gz);
        return CommonResult.success(resuleMap);
    }
    @AutoLog(value = "行业的统计模块")
    @GetMapping("/hytj")
    //@RequiresPermissions("fireInformation/list")
    public CommonResult<Map<String,Object>> hytj(String benginTime, String endTime) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        List<Enterprise> list=enterpriseService.queryUnitSelect(loginUserVo.getEnterpriseId().toString());
        List<Map<String,String>> strList=new ArrayList<>();
        List<String> nameList=new ArrayList<>();
        List<String> codeList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SysDirectory sysDirectory=directoryService.queryByCode(list.get(i).getIndustryCode().toString());
            String name=sysDirectory.getValue();
            String code=sysDirectory.getCode();
            if(!nameList.contains(name)){
                Map<String,String> map=new HashMap<>();
                map.put("code",code);
                map.put("name",name);
                strList.add(map);
                nameList.add(name);
                codeList.add(code);
            }
        }
        Map<String,Object> resuleMap=new HashMap<>();
        resuleMap.put("hy",strList);
        List<Integer> hj=new ArrayList<>();
        List<Integer> gz=new ArrayList<>();
        for (int i = 0; i < strList.size(); i++) {
            Map<String,String> map=strList.get(i);
            List<Enterprise> listEnterprise=enterpriseService.queryListByIndustryCode(map.get("code").toString(),0);
            List<String> eIdList=new ArrayList<>();
            for (int j = 0; j < listEnterprise.size(); j++) {
                if(codeList.contains(listEnterprise.get(j).getIndustryCode())){
                    eIdList.add(listEnterprise.get(j).getId().toString());
                }
            }
            DevHisResult devHisResult=devHisResultService.getCenterNum(eIdList,benginTime,endTime);
            if(null==devHisResult){
                hj.add(0);
                gz.add(0);
            }else{
                hj.add(devHisResult.getFireAlarm());
                gz.add(devHisResult.getFault());
            }
        }
        resuleMap.put("hj",hj);
        resuleMap.put("gz",gz);
        return CommonResult.success(resuleMap);
    }

}
